//
//  MusicModel.m
//  Blue-toothLight
//
//  Created by mazg on 15/6/16.
//  Copyright (c) 2015年 mazg. All rights reserved.
//

#import "MusicModel.h"
#import <MediaPlayer/MediaPlayer.h>

@implementation MusicModel

+(NSMutableArray *)allMusicsFromLocal{
    NSMutableArray *arrM=[[NSMutableArray alloc]init];
    
    MPMediaQuery *everything = [[MPMediaQuery alloc] init];
    NSArray *itemsFromGenericQuery = [everything items];
    for (MPMediaItem *song in itemsFromGenericQuery)
    {
        //type等于1的时候是音乐文件
        if (song.mediaType==1) {
            MusicModel *model=[[MusicModel alloc]init];
            model.songUrl=[song valueForProperty:MPMediaItemPropertyAssetURL];
            model.title = song.title;
            model.artist = song.artist;
            model.playbackDuration=song.playbackDuration;
            model.playingTime=0;
            model.isSelected=NO;
            [arrM addObject:model];
        }
    }
    return arrM;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.title forKey:@"title"];
    [aCoder encodeObject:self.artist forKey:@"artist"];
    [aCoder encodeDouble:self.playbackDuration forKey:@"playbackDuration"];
    [aCoder encodeDouble:self.playingTime forKey:@"playingTime"];
    [aCoder encodeBool:self.isSelected forKey:@"isSelected"];
    [aCoder encodeObject:self.songUrl forKey:@"songUrl"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        self.title = [aDecoder decodeObjectForKey:@"title"];
        self.artist = [aDecoder decodeObjectForKey:@"artist"];
        self.playbackDuration = [aDecoder decodeDoubleForKey:@"playbackDuration"];
        self.playingTime = [aDecoder decodeDoubleForKey:@"playingTime"];
        self.isSelected=[aDecoder decodeObjectForKey:@"isSelected"];
        self.songUrl=[aDecoder decodeObjectForKey:@"songUrl"];
    }
    return self;
}

@end
